package com.kuang.controller;

import com.kuang.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/user")
public class UserController {

    //localhost:8080/user/t1?name=xxx;
    @GetMapping("/t1")
    //@RequestParam("username") : username提交的域的名称//localhost:8080/username/t1?name=xxx;
    public String test1(@RequestParam("username")String name , Model model){
        //1.接收前端参数
        System.out.println("接收到的前端参数为：" + name);
        //2.将返回的结果传输给前端
        model.addAttribute("msg",name);
        //3.视图跳转
        return "test";
    }
    //前端提交一个对象:id name age
    @GetMapping("/t2")
    public String test2(User user){
        System.out.println(user);
        return "test";
    }

    @RequestMapping("/hello")
    public String hello(@RequestParam("username") String name, ModelMap model){
        //封装要显示到视图中的数据
        //相当于req.setAttribute("name",name);
        model.addAttribute("name",name);
        System.out.println(name);
        return "hello";
    }

}
